using System;
using System.Collections.Generic;
using BCW;
using UnityEngine;

namespace BCWAccountVip
{
	public class ManagerVip
	{
		[Serializable]
		public struct ConfigVip
		{
			public int countDays;

			public float cost;

			public ConfigVip(int countDaysValue, float costValue)
			{
				countDays = countDaysValue;
				cost = costValue;
			}
		}

		public const float PERCENT_STAR = 50f;

		public const float PERCENT_CASH = 100f;

		private const int VALID_SHIFT_TIME = 7200;

		public const string EXPIRE_VIP_REMIND = "expirePlayerVipRemind";

		public const string EXPIRE_VIP_SERVER = "expirePlayerVipServer";

		private const string EXPIRE_VIP_LOCAL = "expirePlayerVipLocal";

		private const string LAST_LOCAL_TIMESTAMP = "lastLocalTimestamp";

		private const string THIS_IS_CHEETER = "thisIsCheeter";

		private static ManagerVip instance;

		private bool _initIsCheeter;

		private bool _isCheeter;

		private Dictionary<string, ConfigVip> _configVips = new Dictionary<string, ConfigVip>();

		public bool IsCheeter
		{
			get
			{
				if (!_initIsCheeter)
				{
					_isCheeter = BCWStorage.Get<bool>("thisIsCheeter");
				}
				return _isCheeter;
			}
			set
			{
				_isCheeter = value;
				_initIsCheeter = true;
			}
		}

		public Dictionary<string, ConfigVip> ConfigVips
		{
			get
			{
				return _configVips;
			}
		}

		public static event Action changeVipTime;

		private ManagerVip()
		{
			InitLocalExpire();
			RefresIsCheeter();
		}

		public void InitConfig(List<int> daysConfig, List<iapInfo> IAPsConfig)
		{
			int num = Mathf.Min(daysConfig.Count, IAPsConfig.Count);
			for (int i = 0; i < num; i++)
			{
				ConfigVip value = new ConfigVip(daysConfig[i], IAPsConfig[i].cost);
				if (!_configVips.ContainsKey(IAPsConfig[i].id))
				{
					_configVips.Add(IAPsConfig[i].id, value);
				}
				else
				{
					_configVips[IAPsConfig[i].id] = value;
				}
			}
		}

		public static ManagerVip GetInstance()
		{
			if (instance == null)
			{
				instance = new ManagerVip();
			}
			return instance;
		}

		public bool IsActiveVip()
		{
			return !IsCheeter && HasVip();
		}

		public bool HasVip()
		{
			if (JSONManager.Instance.GetServerTime() != 0)
			{
				return BCWStorage.Get<int>("expirePlayerVipServer") > JSONManager.Instance.GetServerTime();
			}
			return BCWStorage.Get<int>("expirePlayerVipLocal") > BCWServer.GetLocalUnixTime();
		}

		public int GetExpire()
		{
			if (JSONManager.Instance.GetServerTime() != 0)
			{
				return BCWStorage.Get<int>("expirePlayerVipServer");
			}
			return BCWStorage.Get<int>("expirePlayerVipLocal");
		}

		public int GetSeconds()
		{
			if (JSONManager.Instance.GetServerTime() != 0)
			{
				return Mathf.Max(0, GetExpire() - JSONManager.Instance.GetServerTime());
			}
			return Mathf.Max(0, GetExpire() - BCWServer.GetLocalUnixTime());
		}

		public bool AddDays(int days)
		{
			return AddSeconds(days * 3600 * 24);
		}

		public bool AddSeconds(int seconds)
		{
			int serverTime = JSONManager.Instance.GetServerTime();
			if (serverTime == 0)
			{
				return false;
			}
			int b = BCWStorage.Get<int>("expirePlayerVipServer");
			if (!SetExpire(Mathf.Max(serverTime, b) + seconds))
			{
				return false;
			}
			if (GpgWrapper.instance != null)
			{
				GpgWrapper.instance.SaveAccountVip();
			}
			return true;
		}

		public void RestoreExpire(int expire)
		{
			if (expire > BCWStorage.Get<int>("expirePlayerVipServer"))
			{
				SetExpire(expire);
				BCWDebug.Log("restore icloud vip: " + expire, "GPG");
			}
		}

		public int GetExpireVipServer()
		{
			return BCWStorage.Get<int>("expirePlayerVipServer");
		}

		private bool SetExpire(int expire)
		{
			if (!BCWStorage.Set("expirePlayerVipServer", expire, true))
			{
				return false;
			}
			if (ManagerVip.changeVipTime != null)
			{
				ManagerVip.changeVipTime();
			}
			InitLocalExpire();
			PlayerPrefs.SetInt("expirePlayerVipRemind", 1);
			return true;
		}

		private void InitLocalExpire()
		{
			if (JSONManager.Instance.GetServerTime() != 0)
			{
				BCWStorage.Set("thisIsCheeter", false);
				_isCheeter = false;
				if (HasVip())
				{
					int localUnixTime = BCWServer.GetLocalUnixTime();
					BCWStorage.Set("thisIsCheeter", false);
					_isCheeter = false;
					BCWStorage.Set("expirePlayerVipLocal", localUnixTime + GetSeconds());
					BCWStorage.Set("lastLocalTimestamp", localUnixTime);
				}
			}
		}

		private void RefresIsCheeter()
		{
			if (!IsCheeter && HasVip())
			{
				int num = BCWStorage.Get<int>("lastLocalTimestamp");
				int localUnixTime = BCWServer.GetLocalUnixTime();
				_isCheeter = num != 0 && localUnixTime + 7200 < num;
				BCWStorage.Set("thisIsCheeter", _isCheeter, true);
				BCWStorage.Set("lastLocalTimestamp", localUnixTime, true);
			}
		}
	}
}
